package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.entity.SysDistricts;
import com.info33.platform.system.service.SysDistrictsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysDistricts")
@Api(tags = "系统 - 中国省市区行政信息")
public class SysDistrictsController extends BaseController<SysDistrictsService, SysDistricts> {

    private final SysDistrictsService sysDistrictsService;

    @Autowired
    public SysDistrictsController(SysDistrictsService sysDistrictsService) {
        super(sysDistrictsService);
        this.sysDistrictsService = sysDistrictsService;
    }

    @PostMapping(value = "/pc/list")
    @ApiOperation(value = "pc - 下拉列表", notes = "此 api 不接入租户判断")
    @Cacheable(value = "SysDistricts:/pc/list", key = "#entity.pid")
    public Result<List<SysDistricts>> pcList(
            @RequestBody SysDistricts entity) {
        return Result.ok(sysDistrictsService.list(new QueryWrapper<>(entity).orderByAsc("region_code")));
    }

    @PostMapping("/refresh")
    @ApiOperation(value = "pc - 更新行政区域信息")
    @RequiresPermissions(value = "SysDictionary:update", logical = Logical.OR)
    @CacheEvict(value = "SysDistricts::/pc/list", allEntries = true)
    public Result<Object> refresh() {
        return sysDistrictsService.refresh();
    }

    @GetMapping("/translateDistrictsId/{id}")
    public Result<?> translateDistrictsId(@PathVariable String id) {
        return Result.ok(10000, sysDistrictsService.translateDistrictsId(id));
    }

}